<?php

namespace app\api\controller;

use think\Controller;

class Other extends Controller
{
    /**
     * 首页banner
     */
    public function banner($header, $data, $user)
    {
        $banner = db('banner')
            ->where(array('b_type' => 0, 'b_status' => 1))
            ->order("b_sort desc")
            ->limit("0,10")
            ->select();
        $data1 = array();
        foreach ($banner as $k => $v) {
            $data1[] = array(
                'title' => $v['b_title'],//标题
                'img_url' => $v['b_img'],//图片地址
                'url' => $v['b_url'],//链接地址/商品id
                'type'=>$v['b_mold'],//类型：1：链接地址；2：商品id；
            );
        }
        return array('code' => 1, 'msg' => 'banner查询成功', 'data' => $data1);
    }

    /**
     * 首页分类
     */
    public function index_type($header, $data, $user)
    {
        $type = db('goods_type')
            ->where(array('gt_is_index' => 1, 'gt_status' => 1, 'gt_type' => 1))
            ->order("gt_sort desc")
            ->limit("0,10")
            ->select();
        $data1 = array();
        foreach ($type as $k => $v) {
            $data1[] = array(
                'id' => $v['gt_id'],//分类id
                'name' => $v['gt_name'],//分类名称
                'img_url' => $v['gt_img'],//图片地址
            );
        }
        return array('code' => 1, 'msg' => '分类查询成功', 'data' =>$data1);
    }

    /**
     * 服务地区
     */
    public function work_address($header, $data, $user)
    {
        if(empty($data['id'])){
            $id=0;
        }else{
            $id=$data['id'];
        }
        $place = db('place')
            ->where(array('p_status' => 1,'p_id'=>$id))
            ->order("p_sort desc,id")
            ->select();
        $ids='';
        foreach($place as $v){
            $ids.=$v['id'].',';
        }
        $ids=substr($ids,0,-1);
        $place2 = db('place')
            ->where(array('p_status' => 1,'p_id'=>array('in',$ids)))
            ->order("p_sort desc,id")
            ->select();
        $data1=array();
        foreach($place as $k=>$v){
            $data1[$k]['id']=$v['id'];
            $data1[$k]['name']=$v['p_name'];
            foreach($place2 as $vv){
                if($vv['p_id']==$v['id']){
                    $data1[$k]['children'][]=array('id'=>$vv['id'],'name'=>$vv['p_name']);
                }
            }
        }
        return array('code' => 1, 'msg' => '可收货地区', 'data' => $data1);
    }
    //可积分兑换的优惠券
    function coupon(){
        $coupon=db('coupon a')
            ->field("a.*,b.c_name")
            ->join("fr_coupon b","a.c_id = b.c_id")
            ->where(array('a.c_status'=>1,'a.c_type'=>2,'a.c_end_time'=>array('gt',time()+24*3600),'a.c_start_time'=>array('lt',time())))
            ->select();
        $coupon1=array();
        foreach($coupon as $k=>$v){
            $coupon1[]=array(
                'c_id'=>$v['c_id'],
                'name'=>$v['c_name'],
                'need_integral'=>$v['c_need_integral'],
                'discount'=>$v['c_discount'],
                'min_buy'=>$v['c_min_buy'],
                'start_day'=>date('Y.m.d',$v['c_start_time']),
                'end_day'=>date('Y.m.d',$v['c_end_time']),
            );
        }
        return array('code' => 1, 'msg' => '分类查询成功', 'data' =>$coupon1);
    }
    /**
     * 服务地区
     */
    public function system_address($header, $data, $user)
    {
        $address=db("pickup_address")
            ->where(array("pa_status"=>1))
            ->order("pa_sort desc")
            ->select();
        $data1 = array();
        foreach ($address as $k => $v) {
            $data1[] = array(
                'id' => $v['pa_id'],//提货地址id
                'name' => $v['pa_name'],//提货人员名称
                'mobile' => $v['pa_mobile'],//提货人员电话
                'place' => $v['pa_address'],//详细地址
            );
        }
        return array('code' => 1, 'msg' => '收货地址查询成功', 'data' => $data1);
    }
    /**
     * 提货、收货时间
     */
    public function useful_time($header, $data, $user)
    {
        $system=db('system')
            ->where(array('id'=>1))
            ->find();
        $time=time();
        if(date('H')<$system['end_h']){
            for($i=1;$i<=7;$i++){
                $day[]=date('Y-m-d',$time);
                $time=$time+3600*24;
            }
            $is_first=1;
        }else{
            for($i=1;$i<=7;$i++){
                $time=$time+3600*24;
                $day[]=date('Y-m-d',$time);
            }
            $is_first=0;
        }
        $data1['day']=$day;//日期
        $data1['time']=array(array('id'=>1,'name'=>$system['am_delivery_time']),array('id'=>2,'name'=>$system['pm_delivery_time']));
        //上午、下午收货时间
        $data1['is_first']=$is_first;//是否第一天只能选第一条下午：1：第一天只能选下午；0：可选全部；
        return array('code' => 1, 'msg' => '提货、收货时间', 'data' => $data1);
    }
    /**
     * 常见问题列表
     */
    public function problem_list($header, $data, $user)
    {
        $problem=db('problem')
            ->field("id,p_title,p_content")
            ->where(array('p_status'=>1))
            ->order("p_sort desc")
            ->select();
        $problem1=array();
        foreach($problem as $v){
            $problem1[]=array('id'=>$v['id'],'title'=>$v['p_title'],'content'=>$v['p_content']);
        }
        return array('code' => 1, 'msg' => '常见问题列表', 'data' => $problem1);
    }
    /**
     * 常见问题详情
     */
    public function problem_details($header, $data, $user)
    {
        $check = check_array($data, array('id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $id=$data['id'];
        $problem=db('problem')
            ->where(array('p_status'=>1,'id'=>$id))
            ->find();
        if(empty($problem)){
            return array('code' => 2, 'msg' => '没有该常见问题', 'data' => array());
        }
        $problem1=array('id'=>$problem['id'],'title'=>$problem['p_title'],'create_time'=>date('Y-m-d H:i:s',$problem['p_create_time']),'content'=>$problem['p_content']);
        return array('code' => 1, 'msg' => '常见问题详情', 'data' => array('problem'=>$problem1));
    }
    /**
     * 热门搜索词
     */
    public function hot_search($header, $data, $user)
    {
        $system=db('system')->find();
        $hot_search=explode('|',$system['hot_search']);
        return array('code' => 1, 'msg' => '热门搜索词', 'data' => $hot_search);
    }
}